package com.bosssoft.itfinance.citizen.smsclient.risk;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 风控
 * @author ZhiXy
 * @since 2018-04-26 17:39
 */
public class RiskUtil {
    public static final String RISK_MAP_PREFIX = "risk_";
    private static Map<String, RiskBo> riskMap = new HashMap<>();

    public static Boolean judge(String authName) {
        Long nowTimeStamp = new Date().getTime();

        RiskBo riskBo = riskMap.get(RISK_MAP_PREFIX + authName);
        if (riskBo == null) {
            RiskBo first = new RiskBo();
            first.setSmsCount(1);
            first.setTimeStamp(new Date().getTime());
            riskMap.put(RISK_MAP_PREFIX + authName, first);
            return true;
        }
        if ((nowTimeStamp - riskBo.getTimeStamp()) < RiskBo.INTERVAL_TIME_STAMP) {
            if (riskBo.getSmsCount() < RiskBo.SMS_MAX_COUNT) {
                riskBo.setSmsCount(riskBo.getSmsCount() + 1);
                return true;
            }
        } else {
            riskBo.setTimeStamp(nowTimeStamp);
            riskBo.setSmsCount(1);
            return true;
        }

        return false;
    }
}
